<?php

namespace app\api\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Config;
// use think\facade\Session;
use app\common\model\SystemAdmin;
use app\common\model\User;
use app\common\model\Order;
use app\common\model\OrderUser;
use app\common\controller\Handle;
use app\common\model\Store;
use app\common\model\Product;
use app\common\model\ProductType;
use think\facade\Request;

class ApiOrder extends ApiBase
{
    public function initialize()
    {
        parent::initialize();
        $this->user = new User;
        $this->Order = new Order;
        $this->Store = new Store;
        $this->Product = new Product;
        $this->ProductType = new ProductType;
    }

    

    //添加订单
    public function addOrder()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();


        if(empty($data['name'])){
            return json(['status' => 0, 'msg' =>"请上传客户名称"]);
        }

        if(empty($data['address'])){
            return json(['status' => 0, 'msg' =>"请上传客户地址"]);
        }

        if(empty($data['tel'])){
            return json(['status' => 0, 'msg' =>"请上传客户联系电话"]);
        }

        if(empty($data['riqi'])){
            return json(['status' => 0, 'msg' =>"请选择日期"]);
        }

        if(empty($data['time_frame'])){
            return json(['status' => 0, 'msg' =>"请选择时段"]);
        }

        if(empty($data['z_price'])){
            return json(['status' => 0, 'msg' =>"请上传总价"]);
        }

        if(empty($data['earnest_price'])){
            return json(['status' => 0, 'msg' =>"请上传定金"]);
        }

        if(empty($data['status'])){
            return json(['status' => 0, 'msg' =>"请上传支付状态"]);
        }

        if(empty($data['cost_price'])){
            return json(['status' => 0, 'msg' =>"请上传成本价格"]);
        }

        if(empty($data['type'])){
            return json(['status' => 0, 'msg' =>"请上传客户来源"]);
        }
        
        $time = strtotime($data['riqi']);

        $YYY = date("Y",$time);
        $MMM = date("m",$time);
        $DDD = date("d",$time);

        $add = [
            "name"=>$data['name'],
            "address"=>$data['address'],
            "tel"=>$data['tel'],
            "riqi"=>$data['riqi'],
            "time_frame"=>$data['time_frame'],
            "z_price"=>$data['z_price'],
            "earnest_price"=>$data['earnest_price'],
            "status"=>$data['status'],
            "cost_price"=>$data['cost_price'],
            "created_at"=>date("Y-m-d H:i:s"),
            "update_at"=>date("Y-m-d H:i:s"),
            "store_id"=> $store_id,
            "YYY"=> $YYY,
            "MMM"=> $MMM,
            "DDD"=> $DDD,
            "type"=> $data['type'],
        ];

        if(!empty($data['remark'])){
            $add['remark'] = $data['remark'];
        }
        if(!empty($data['product_img'])){
            $add['product_img'] = $data['product_img'];
        }
        if(!empty($data['product_name'])){
            $add['product_name'] = $data['product_name'];
        }
        if(!empty($data['product_content'])){
            $add['product_content'] = $data['product_content'];
        }

        if(!empty($data['product_id'])){
            $add['product_id'] = $data['product_id'];
        }

        $res = $this->Order->create($add);
        
        return json(['status' => 1, 'msg' => '操作成功']);

    }
    // 获取某年某月某日的订单列表
    public function listOrderDay()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];
        $data = request()->post();


        if(!empty($data['year'])){
            $year = $data['year'];
        }else{
             $year = date("Y");
        }

        if(!empty($data['month'])){
            $month = $data['month'];
        }else{
            $month = date("m");
        }

        if(!empty($data['day'])){
            $day = $data['day'];
        }else{
            $day = date("d");
        }

        $list = [];

        // 获取当前月的订单信息
        // 存储空间对应的域名
        $domain = config('sysconfig.qiniu_domain');
        $list['data'] =  $this->Order->getAllOrder(["store_id"=>$store_id,"YYY"=>$year,"MMM"=>$month,'DDD'=>$day],"id,name,z_price,earnest_price,product_img,address,tel,YYY,MMM,DDD,time_frame,type","time_frame asc")->toArray();
        foreach($list['data'] as &$v){
            // $v['product_img'] = $domain."/".$v['product_img'];
            // $val->logo = $domain."/".$val['logo'];
        //     // $val->type_name = $this->ProductType->where(['id'=>$val['type_id']])->value("name");
            
            
        }
        $list['year'] = $year;
        $list['month'] = $month;
        $list['day'] = $day;

        return json(['status' => 1, 'msg' => '操作成功',"data"=>$list]);

    }
    // 订单列表
    public function listOrder()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];
        $data = request()->post();


        if(!empty($data['year'])){
            $year = $data['year'];
        }else{
             $year = date("Y");
        }

        if(!empty($data['month'])){
            $month = $data['month'];
        }else{
            $month = date("m");
        }

        $list = [];

        // 获取当前月的订单信息
        // $listOne =  $this->Order->getAllOrder(["store_id"=>$store_id,"YYY"=>$year,"MMM"=>$month],"id,name,address,tel,YYY,MMM,DDD,time_frame,type","time_frame asc")->toArray();
        
        $list['data'] =[];
        $orderlist=  $this->Order->where(["store_id"=>$store_id,"YYY"=>$year,"MMM"=>$month])->field('count(id) as num,DDD')->group('DDD')->select();
        foreach($orderlist as $v){
            $list['data'][(int)$v['DDD']] = $v['num'];
        }
        $list['year'] = $year;
        $list['month'] = $month;
        // foreach($listOne as $kOne=>$vOne){
        //     $listOneNew[$vOne['DDD']] = $vOne;
        // }
      
        $dateOne = $year."-".$month."-01";
        // 获取当月天数
        $list['days'] = (int)date("t",strtotime($dateOne));
        $list['before'] = (int)date('N',strtotime($dateOne))%7;

        // $list[0]["year"] = $year;
        // $list[0]["month"] = $month;

        // for($i=1;$i<$daysOne;$i++){
        //     if(!empty($listOneNew[$i])){
        //         $list[0][$i] = $listOneNew[$i];
        //     }else{  
        //         $list[0][$i] = [];
        //     }
            
        // }

    //     // 下一个月的订单
    //     $monthTwo = $month + 1;
    //     if($monthTwo  > 12){
    //         $monthTwo = 1;
    //         $yearTwo = $year + 1;
    //     }else{
    //         $yearTwo = $year;
    //     }

    //     // 获取当前月的订单信息
    //     $listTwo =  $this->Order->getAllOrder(["store_id"=>$store_id,"YYY"=>$yearTwo,"MMM"=>$monthTwo],"id,name,address,tel,YYY,MMM,DDD,time_frame,type","time_frame asc")->toArray();
    //     foreach($listTwo as $kTwo=>$vTwo){
    //         $listTwoNew[$vTwo['DDD']] = $vTwo;
    //     }

    //     $dateTwo = $yearTwo."-".$monthTwo."-01";
    //     // 获取下一个月天数
    //     $daysTwo = date("t",strtotime($dateTwo));

    //     $list[1]["year"] = $yearTwo;
    //     $list[1]["month"] = $monthTwo;
      
    //     for($i=1;$i<$daysTwo;$i++){
    //         if(!empty($listTwoNew[$i])){
    //             $list[1][$i] = $listTwoNew[$i];
    //         }else{  
    //             $list[1][$i] = [];
    //         }
    //     }

    //      // 下二个月的订单
    //      $monthThree = $monthTwo + 1;
    //      if($monthThree  > 12){
    //          $monthThree = 1;
    //          $yearThree = $yearTwo + 1;
    //      }else{
    //          $yearThree = $yearTwo;
    //      }
         
    //     // 获取当前月的订单信息
    //     $listThree =  $this->Order->getAllOrder(["store_id"=>$store_id,"YYY"=>$yearThree,"MMM"=>$monthThree],"id,name,address,tel,YYY,MMM,DDD,time_frame,type","time_frame asc")->toArray();
    //     foreach($listThree as $kThree=>$vThree){
    //         $listThreeNew[$vThree['DDD']] = $vThree;
    //     }

    //     $dateThree = $yearThree."-".$monthThree."-01";
    //     // 获取下一个月天数
    //     $daysThree = date("t",strtotime($dateThree));

    //     $list[2]["year"] = $yearThree;
    //     $list[2]["month"] = $monthThree;

    //     for($i=1;$i<$daysThree;$i++){
    //         if(!empty($listThreeNew[$i])){
    //             $list[2][$i] = $listThreeNew[$i];
    //         }else{  
    //             $list[2][$i] = [];
    //         }
    //     }


    //     // 下三个月的订单
    //     $monthFour = $monthThree + 1;
    //     if($monthFour  > 12){
    //         $monthFour = 1;
    //         $yearFour = $yearThree + 1;
    //     }else{
    //         $yearFour = $yearThree;
    //     }
        
    //    // 获取当前月的订单信息
    //     $listFour =  $this->Order->getAllOrder(["store_id"=>$store_id,"YYY"=>$yearFour,"MMM"=>$monthFour],"id,name,address,tel,YYY,MMM,DDD,time_frame,type","time_frame asc")->toArray();
    //     foreach($listFour as $kFour=>$vFour){
    //         $listFourNew[$vFour['DDD']] = $vFour;
    //     }

    //     $dateFour = $yearFour."-".$monthFour."-01";
    //     // 获取下一个月天数
    //     $daysFour = date("t",strtotime($dateFour));

    //     $list[3]["year"] = $yearFour;
    //     $list[3]["month"] = $monthFour;

    //     for($i=1;$i<$daysFour;$i++){
    //         if(!empty($listFourNew[$i])){
    //             $list[3][$i] = $listFourNew[$i];
    //         }else{  
    //             $list[3][$i] = [];
    //         }
    //     }

    //     // 下四个月的订单
    //     $monthFive = $monthFour + 1;
    //     if($monthFive  > 12){
    //         $monthFive = 1;
    //         $yearFive = $yearFour + 1;
    //     }else{
    //         $yearFive = $yearFour;
    //     }
       
    //     // 获取当前月的订单信息
    //     $listFive =  $this->Order->getAllOrder(["store_id"=>$store_id,"YYY"=>$yearFive,"MMM"=>$monthFive],"id,name,address,tel,YYY,MMM,DDD,time_frame,type","time_frame asc")->toArray();
    //     foreach($listFive as $kFive=>$vFive){
    //             $listFiveNew[$vFive['DDD']] = $vFive;
    //     }
    //     $dateFive = $yearFive."-".$monthFive."-01";
    //     // 获取下一个月天数
    //     $daysFive = date("t",strtotime($dateFive));

    //     $list[4]["year"] = $yearFive;
    //     $list[4]["month"] = $monthFive;

    //     for($i=1;$i<$daysFive;$i++){
    //         if(!empty($listFiveNew[$i])){
    //             $list[4][$i] = $listFiveNew[$i];
    //         }else{  
    //             $list[4][$i] = [];
    //         }
    //     }


    //     // 下五个月的订单
    //     $monthSix = $monthFive + 1;
    //     if($monthSix  > 12){
    //         $monthSix = 1;
    //         $yearSix = $yearFive + 1;
    //     }else{
    //         $yearSix = $yearFive;
    //     }
      
    //     // 获取当前月的订单信息
    //     $listSix =  $this->Order->getAllOrder(["store_id"=>$store_id,"YYY"=>$yearSix,"MMM"=>$monthSix],"id,name,address,tel,YYY,MMM,DDD,time_frame,type","time_frame asc")->toArray();
    //     foreach($listSix as $kSix=>$vSix){
    //         $listSixNew[$vSix['DDD']] = $vSix;
    //     }
    //     $dateSix = $yearSix."-".$monthSix."-01";
    //     // 获取下一个月天数
    //     $daysSix = date("t",strtotime($dateSix));

    //     $list[5]["year"] = $yearSix;
    //     $list[5]["month"] = $monthSix;

    //     for($i=1;$i<$daysSix;$i++){
    //         if(!empty($listSixNew[$i])){
    //             $list[5][$i] = $listSixNew[$i];
    //         }else{  
    //             $list[5][$i] = [];
    //         }
    //     }

        return json(['status' => 1, 'msg' => '操作成功',"data"=>$list]);

    }

    // 订单详情
    public function getOrder()
    {
        // 存储空间对应的域名
        $domain = config('sysconfig.qiniu_domain');

        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();

        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传订单ID"]);
        }

        $list = $this->Order->getOne(['id' =>$data['id']]);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"订单不存在"]);
        }

        if(!empty($list['content'])){
          $list['content'] = str_replace("api_order",$domain."/api_order",$list['content']);
        }

        // if(!empty($list['product_img'])){
        //     $list['product_img'] = $domain."/".$list['product_img'];
        // }

        
        return json(['status'=> 1,'data'=>$list]);
    }


    // 修改订单
    public function updateOrder()
    {
        // 存储空间对应的域名
        $domain = config('sysconfig.qiniu_domain');

        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();

        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传订单ID"]);
        }

        $list = $this->Order->getOne(['id' =>$data['id']]);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"订单不存在"]);
        }


        if(empty($data['name'])){
            return json(['status' => 0, 'msg' =>"请上传客户名称"]);
        }

        if(empty($data['address'])){
            return json(['status' => 0, 'msg' =>"请上传客户地址"]);
        }

        if(empty($data['tel'])){
            return json(['status' => 0, 'msg' =>"请上传客户联系电话"]);
        }

        if(empty($data['riqi'])){
            return json(['status' => 0, 'msg' =>"请选择日期"]);
        }

        if(empty($data['time_frame'])){
            return json(['status' => 0, 'msg' =>"请选择时段"]);
        }

        if(empty($data['z_price'])){
            return json(['status' => 0, 'msg' =>"请上传总价"]);
        }

        if(empty($data['earnest_price'])){
            return json(['status' => 0, 'msg' =>"请上传定金"]);
        }

        if(empty($data['status'])){
            return json(['status' => 0, 'msg' =>"请上传支付状态"]);
        }

        if(empty($data['cost_price'])){
            return json(['status' => 0, 'msg' =>"请上传成本价格"]);
        }

        if(empty($data['type'])){
            return json(['status' => 0, 'msg' =>"请上传客户来源"]);
        }
        
        $time = strtotime($data['riqi']);

        $YYY = date("Y",$time);
        $MMM = date("m",$time);
        $DDD = date("d",$time);

        $save = [
            "name"=>$data['name'],
            "address"=>$data['address'],
            "tel"=>$data['tel'],
            "riqi"=>$data['riqi'],
            "time_frame"=>$data['time_frame'],
            "z_price"=>$data['z_price'],
            "earnest_price"=>$data['earnest_price'],
            "status"=>$data['status'],
            "cost_price"=>$data['cost_price'],
            "created_at"=>date("Y-m-d H:i:s"),
            "update_at"=>date("Y-m-d H:i:s"),
            "store_id"=> $store_id,
            "YYY"=> $YYY,
            "MMM"=> $MMM,
            "DDD"=> $DDD,
            "type"=> $data['type'],
        ];

        if(!empty($save['content'])){
            $save['content'] = str_replace($domain."/api_order","api_order",$list['content']);
        }

        if(!empty($data['remark'])){
            $save['remark'] = $data['remark'];
        }else{
            $save['remark'] = "";
        }

        // if(!empty($data['product_id'])){
        //     $list = $this->Product->getOne(['id'=> $data['product_id'],"deleted_at"=>null]);

        //     $save['product_img'] = $list['logo'];
        //     $save['product_content'] = $list['content'];

        // }else{
        //     if(!empty($data['product_img'])){
        //         $save['product_img'] = $data['product_img'];
        //     }else{
        //         $save['product_img'] = "";
        //     }

        //     if(!empty($data['product_content'])){
        //         $save['product_content'] = $data['product_content'];
        //     }else{
        //         $save['product_img'] = "";
        //     }
        // }


        if(!empty($data['product_img'])){
            $save['product_img'] = $data['product_img'];
        }
        if(!empty($data['product_name'])){
            $save['product_name'] = $data['product_name'];
        }
        if(!empty($data['product_content'])){
            $save['product_content'] = $data['product_content'];
        }

        if(!empty($data['product_id'])){
            $save['product_id'] = $data['product_id'];
        }

        $res = $this->Order->where(['id' =>$data['id']])->update($save);
        
        return json(['status' => 1, 'msg' => '操作成功',$data['product_content']]);
        
    }

    // 删除订单
    public function delOrder()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();

        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传订单ID"]);
        }

        $list = $this->Order->getOne(['id' =>$data['id']]);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"订单不存在"]);
        }

        $save['deleted_at'] = date("Y-m-d H:i:s");

        $res = $this->Order->where(['id' =>$data['id']])->update($save);
        
        return json(['status' => 1, 'msg' => '操作成功']);
  
    }

      

    // 上传商品图片或者视频
    public function addFile()
    {
        $file = request()->file('file');

        if (!$file) {
            return ['status' => 0, 'msg' => '上传失败，图片不存在'];
        }

        // 获取文件原始扩展名
        $extension = $file->extension();

        $hand = new Handle();

        $dirname = "api_order";

        $savename = $hand->qiniuImg("file", $dirname);

        $add['file_name'] = $savename['fileName'];
        $add['upload_type'] = 2;
        $add['file_path'] = $savename['url'];
        $add['file_type'] = $extension;
        $add['ip'] = Request::ip();
        $add['add_time'] = time();
        $add['user_id'] = $this->uid;
        
        $res = $this->FileLog->create($add);
        $fileLogId = $res->id;
        $savename['file_id'] = $fileLogId;

        return  json($savename);
    }

  

   
    
}
